package dubbo.server2.impl.controller;

import dubbo.server1.api.TestService1;
import dubbo.server1.api.entity.User;
import dubbo.server2.impl.common.FastDFSClient;
import dubbo.server3.api.TestService3;
import dubbo.server4.api.TestService4;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloWordController {
	private Logger logger = Logger.getLogger(HelloWordController.class);
	@Autowired
	private TestService1 testService1;
	@Autowired
	private TestService4 testService4;
	@Autowired
	private TestService3 testService3;
	/*
	 * http://localhost:8082/hello
	 */

	@RequestMapping("/hello")
	public String greeting() {
		logger.info("hello2");
		return "hello";
	}

	@RequestMapping("/call")
	@ResponseBody
	public List<String> call() {
		logger.info("call2");
		List<String> list = new ArrayList<>();
		if (testService1 != null)
			list.add("Call Success:" + testService1.getServerName());
		if (testService4 != null)
			list.add("Call Success:" + testService4.getServerName());
		if (testService3 != null)
			list.add("Call Success:" + testService3.getServerName());
		return list;
	}

	@RequestMapping(consumes = "multipart/form-data", value = "upload", method = RequestMethod.POST)
	public ModelAndView uploadFile(List<MultipartFile> Filedata) throws IOException {
		ModelAndView view=new ModelAndView( "hello");
		String name = Filedata.get(0).getOriginalFilename();
		String path = FastDFSClient.getInstance().uploadFile(Filedata.get(0).getBytes(), name);
		view.addObject("imgUrl", path);
		return view;
	}
	@RequestMapping("/jpa")
	@ResponseBody
	public List<User> jpa() {
		return testService1.getUser();
	}
}
